package com.ruoyi.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.domain.TestKnowledge;
import com.ruoyi.domain.vo.TestKnowledgeVo;
import com.ruoyi.domain.vo.TestSubjectGradeVo;
import com.ruoyi.service.ITestKnowledgeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 考试平台知识点Controller
 *
 */
@Api(tags = "知识点管理接口")
@RestController
@RequestMapping("/exam/knowledge")
public class TestKnowledgeController extends BaseController
{
    @Autowired
    private ITestKnowledgeService testKnowledgeService;

    /**
     * 查询考试平台知识点列表
     */
    @ApiOperation("查询考试平台知识点列表")
    @GetMapping("/list")
    public AjaxResult list(String grade)
    {
        startPage();
        List<TestSubjectGradeVo> list = testKnowledgeService.selectTestKnowledgeList(grade);
        return success(list);
    }

    /**
     * 获取考试平台知识点详细信息
     */
    @GetMapping(value = "/{knowlId}")
    public AjaxResult getInfo(@PathVariable("knowlId") Long knowlId)
    {
        return success(testKnowledgeService.selectTestKnowledgeByKnowlId(knowlId));
    }

    /**
     * 新增考试平台知识点
     */
    @ApiOperation("新增考试平台知识点")
    @PostMapping
    public AjaxResult add(@RequestBody TestKnowledge testKnowledge)
    {
        if(!testKnowledgeService.selectTestKnowledgeListByClass(testKnowledge).isEmpty()){
            return AjaxResult.error("请勿重复添加知识点");
        }else {
            return toAjax(testKnowledgeService.insertTestKnowledge(testKnowledge));
        }
    }

    /**
     * 修改考试平台知识点
     */
    @ApiOperation("修改考试平台知识点")
    @PutMapping
    public AjaxResult edit(@RequestBody TestKnowledge testKnowledge)
    {
        return toAjax(testKnowledgeService.updateTestKnowledge(testKnowledge));
    }

    /**
     * 逻辑删除考试平台知识点
     */
    @ApiOperation("删除考试平台知识点")
    @DeleteMapping
    public AjaxResult remove(@RequestBody TestKnowledge testKnowledge)
    {
        if(testKnowledgeService.selectTestKnowledgeChildId(testKnowledge.getKnowlId()).isEmpty()){
            return toAjax(testKnowledgeService.updateTestKnowledge(testKnowledge));
        }else {
            return AjaxResult.error("无法删除该知识点");
        }
    }
    /**
     * 查询所有考试平台知识点
     */
    @ApiOperation("查询所有考试平台知识点")
    @GetMapping("getKnowledges")
    public AjaxResult getAllSubjects(String grade,String subjectName){
        List<TestKnowledgeVo> list = testKnowledgeService.listWithTree(grade, subjectName);
        System.out.println(list);
        return AjaxResult.success(list);

    }


}
